package com.xh.bussiness.resource.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.course.model.CourseIndex;
import com.xh.bussiness.course.service.ICourseIndexService;
import com.xh.bussiness.resource.bean.dto.MaterialDto;
import com.xh.bussiness.resource.bean.request.MaterialCourseIndexRequest;
import com.xh.bussiness.resource.constant.MaterialType;
import com.xh.bussiness.resource.dao.MaterialCourseIndexMapper;
import com.xh.bussiness.resource.model.MaterialCourseIndex;
import com.xh.bussiness.resource.service.IMaterialCourseIndexService;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

@Service
public class MaterialCourseIndexServiceImpl extends BaseServiceImpl<MaterialCourseIndexMapper, MaterialCourseIndex> implements IMaterialCourseIndexService {

    @Autowired
    private ICourseIndexService courseIndexService;

    @Override
    public List<MaterialCourseIndex> query(MaterialCourseIndexRequest bean) {
        QueryWrapper qw = new QueryWrapper<MaterialCourseIndex>();
        if(!StringUtils.isEmpty(bean.getCourseIndexId())){
            qw.eq("course_index_id",bean.getCourseIndexId());
        }

        if(!StringUtils.isEmpty(bean.getMaterialId())){
            qw.eq("material_id",bean.getMaterialId());
        }
        return this.dao.selectList(qw);
    }



    @Override
    @Transactional
    public boolean add(List<MaterialCourseIndexRequest> bean) {

        //获取所有的课次
        Set<String> courseIds = bean.stream().map(MaterialCourseIndexRequest::getCourseId).collect(Collectors.toSet());
        QueryWrapper<CourseIndex> ciQw = new QueryWrapper<>();
        ciQw.in("course_id",courseIds);
        List<CourseIndex> courseIndexList = courseIndexService.queryByCondition(ciQw);
        List<String> courseIndesIds = courseIndexList.stream().map(CourseIndex::getId).collect(Collectors.toList());
        List<String> materials = new ArrayList<>();

        for (int i = 0; i < bean.size(); i++) {
            MaterialCourseIndexRequest mcir = bean.get(i);
            List<MaterialCourseIndex> list = query(mcir);
            if(CollectionUtils.isEmpty(list)){
                MaterialCourseIndex mci = new MaterialCourseIndex();
                BeanUtils.copyProperties(mcir,mci);
                this.dao.insert(mci);
            }
            courseIndesIds.remove(mcir.getCourseIndexId());
            materials.add(mcir.getMaterialId());
        }

        if(!CollectionUtils.isEmpty(courseIndesIds)){
            QueryWrapper<MaterialCourseIndex> delQw = new QueryWrapper<>();
            delQw.in("course_index_id",courseIndesIds);
            delQw.in("material_id",materials);
            this.dao.delete(delQw);
        }

        return true;
    }

    @Override
    public boolean delete(MaterialCourseIndexRequest bean) {
        QueryWrapper qw = new QueryWrapper<MaterialCourseIndex>();
        if(!StringUtils.isEmpty(bean.getCourseIndexId())){
            qw.eq("course_index_id",bean.getCourseIndexId());
        }

        if(!StringUtils.isEmpty(bean.getMaterialId())){
            qw.eq("material_id",bean.getMaterialId());
        }
        return this.dao.delete(qw) > 0;
    }
    /**
     * 准备条件
     *
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(MaterialCourseIndexRequest bean, QueryWrapper qw) {
        if (bean.getIds() != null && bean.getIds().length > 0) {
            qw.in("id", bean.getIds());
        }
        return qw;
    }

    @Override
    public List<MaterialDto>  queryMaterialByCourseIndex(MaterialCourseIndexRequest bean){

       return  this.dao.queryMaterialByCourseIndex(bean);

    }

    @Override
    public Map<String,Long>  queryMaterialCountByCourseIndex(MaterialCourseIndexRequest bean){

        List<MaterialDto> materialDtos = queryMaterialByCourseIndex(bean);

        Map<String,Long> resMap = new LinkedHashMap<>(16);
        long courseCount =0;
        long handoutCount = 0;
        long mediaCount = 0;
        long otherCount = 0;
        long teachplanCount = 0;
        long multimediaCount = 0;
        if(materialDtos.size() > 0){
            Map<Integer, Long> groupbyMap = materialDtos.stream()
                    // 之前旧数据有没有type的
                    .filter(o -> o.getType() != null)
                    .collect(Collectors.groupingBy(MaterialDto::getType,Collectors.counting()));
            for (Integer key : groupbyMap.keySet()) {
                switch (key){
                    case 1:
                        courseCount += groupbyMap.get(key);
                        break;
                    case 2:
                        handoutCount += groupbyMap.get(key);
                        break;
                    case 3:
                        mediaCount += groupbyMap.get(key);
                        break;
                    case 4:
                        otherCount += groupbyMap.get(key);
                        break;
                    case 5:
                        teachplanCount += groupbyMap.get(key);
                        break;
                    case 6:
                        multimediaCount += groupbyMap.get(key);
                        break;
                    default:
                        break;
                }
            }
        }
        resMap.put(MaterialType.ALL,courseCount + handoutCount + otherCount + mediaCount + teachplanCount + multimediaCount);
        resMap.put(MaterialType.COURSEWARE,courseCount );
        resMap.put(MaterialType.HANDOUT, handoutCount );
        resMap.put(MaterialType.TEACHPLAN, teachplanCount);
        resMap.put(MaterialType.MEDIA, mediaCount);
        resMap.put(MaterialType.OTHER, otherCount);
        resMap.put(MaterialType.MULTIMEDIA, multimediaCount);

        return resMap;
    }

    @Override
    public void deleteByCourseIndex(String courseIndexId) {
        this.dao.deleteByCourseIndex(courseIndexId);
    }


}
